package main

import (
	"context"
	"fmt"
	stdlog "log"
	"web-service-demo/grades"
	"web-service-demo/registry"
	"web-service-demo/service"
)

func main() {
	//设置服务URL
	host, port := "localhost", "6000"
	serviceAddress := fmt.Sprintf("http://%v:%v", host, port)

	//准备注册信息
	r := registry.Registration{
		ServiceName:      "GradingService",
		ServiceURL:       serviceAddress,
		RequiredServices: []string{"LogService"},
		ServiceUpdateURL: serviceAddress + "/services",
		HeartbeatURL:     serviceAddress + "/heartbeat",
	}

	//启动服务
	ctx, err := service.Start(context.Background(), host, port, r, grades.RegisterHandlers)
	if err != nil {
		stdlog.Fatal(err)
	}

	//使用依赖服务
	if logProvider, err := service.GetProvider("LogService"); err == nil {
		fmt.Printf("Logging service found at: %s\n", logProvider)
		//改变log日志打印的地方
		grades.SetClientLogger(logProvider, r.ServiceName)
	}

	//等待程序关闭
	<-ctx.Done()
	fmt.Println("Shutting down grading service")
}
